package com.inspur.edp.common.type;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.inspur.edp.common.type.constant.TypeConstant;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lizhaorui
 * @description TODO
 * @date 2024/5/27 15:34
 */

@Data
public class Field {

    public final static String FIELD_DECORATION_NAME = "dslField";

    public final static String ACCESS_KIND_NAME = "accessKind";

    public final static String JSON_DESERIALIZER_NAME = "jsonDeSerializer";

    public final static String SETTER = "setter";

    public final static String GETTER = "getter";

    public final static String GET_SET_VALUE = "getSet";

    private String code;

    private String name;

    private TypeRefer type;

    private boolean isStatic;

    private ObjectNode valueExpress;

    private Map<String, DecorationOption> decorationMap;

    public void addDecoration(DecorationOption decoration) {
        if (this.decorationMap == null) {
            this.decorationMap = new HashMap<>();
        }
        this.decorationMap.put(decoration.getName(), decoration);
    }

    @JsonIgnore
    public String getAccessKind() {
        if (this.decorationMap == null) {
            return TypeConstant.DEFAULT_VALUE;
        }
        DecorationOption decorationOption = this.decorationMap.get(ACCESS_KIND_NAME);
        if (decorationOption == null) {
            return TypeConstant.DEFAULT_VALUE;
        } else {
            return decorationOption.getOptionMap().get(TypeConstant.KIND);
        }
    }

    @JsonIgnore
    public void setAccessKind(String kind) {
        DecorationOption accessOpt = new DecorationOption(ACCESS_KIND_NAME, new HashMap<>());
        this.addDecoration(accessOpt);
        accessOpt.put("kind", kind);
    }


    @JsonIgnore
    public String getGetter() {
        if (this.decorationMap == null) {
            return null;
        }
        DecorationOption decorationOption = this.decorationMap.get(ACCESS_KIND_NAME);
        if (decorationOption == null) {
            return null;
        } else {
            return decorationOption.getOptionMap().get(GETTER);
        }
    }

    @JsonIgnore
    public String getSetter() {
        if (this.decorationMap == null) {
            return null;
        }
        DecorationOption decorationOption = this.decorationMap.get(ACCESS_KIND_NAME);
        if (decorationOption == null) {
            return null;
        } else {
            return decorationOption.getOptionMap().get(SETTER);
        }
    }


}
